/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.hyjiacan.deadqq.client;


import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;

import org.hyjiacan.deadqq.client.window.MainWindow;
import org.hyjiacan.deadqq.client.window.WindowManager;

/**
 * 系统托盘图标
 *
 * @author hyjiacan
 */
public class XTray {
    // 托盘实例

    private static SystemTray tray;
    // 托盘处的图标
    private static TrayIcon icon;
    // 托盘的图片
    private static Image img;
    // 托盘的菜单
    private static PopupMenu menu;

    // 一个程序中只需要一个托盘图标，其中的内容也不会随着操作的变化而改变，所以将此类设置为静态类
    private XTray() {
    }

    /**
     * 显示托盘，此方法应该在程序启动时就被调用
     */
    public static void show() {
        try {
            // 先判断系统是否支持托盘
            if (SystemTray.isSupported()) {
                // 读取要显示在托盘的图片资源
                img = ImageIO.read(XTray.class.getResource("/com/hyjiacan/xx/client/res/icon.png"));
                
                icon = new TrayIcon(img);
                menu = new PopupMenu();
                tray = SystemTray.getSystemTray();
                
                icon.setImageAutoSize(true);
                icon.setToolTip("XX 1.0");
                icon.setPopupMenu(menu);
                
                icon.addMouseListener(new MouseAdapter() {
                    
                    @Override
                    public void mouseClicked(MouseEvent ev) {
                        if (ev.getClickCount() == 2) {
                            JFrame w = WindowManager.get(MainWindow.class.getName());
                            if (w.isVisible()) {
                                w.setVisible(false);
                            } else {
                                w.setVisible(true);
                            }
                        }
                    }
                });

                // 显示程序主窗口
                MenuItem show = new MenuItem("Show Window");
                // 退出程序
                MenuItem exit = new MenuItem("Exit");
                
                show.addActionListener(new ActionListener() {
                    
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        WindowManager.get(MainWindow.class.getName()).setVisible(true);
                    }
                });
                exit.addActionListener(new ActionListener() {
                    
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        System.exit(0);
                    }
                });
                
                menu.add(show);
                menu.add(exit);
                
                tray.add(icon);
                
            }
        } catch (AWTException ex) {
            Logger.getLogger(XTray.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(XTray.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    
    public void loadIcon(Image img){
        
    }
}
